<template>
    <div v-loading="buttonLoading">

        <CommonPage btnType="submit" :state="state" :flowId="form.flowId" @submitForm="submitForm">
            <AddInfo style="padding: 20px" ref="form" v-model="form" :state="state"></AddInfo>
        </CommonPage>

    </div>
</template>

<script>


import {purchaseAdd, purchaseUpdate, purchaseDetail, queryParamList} from "@/api/purchaseManage/purchaseList";
import AddInfo from "@/views/zcwh/transfer/components/info.vue";
import {findByIds} from "@/api/cwgd/prchaseTaxApi";
import {carTransferAdd, carTransferEdit} from "@/api/zcwh/allocate";

export default {
    name: "TransferAdd",
    components: {AddInfo},
    data() {
        return {
            buttonLoading: false,
            form: {
              carList:[],
              transferAnnex:'',
              feeAnnex:'',
            },
            state: 'write',
        };
    },
    mounted() {
        if (this.$route.query && this.$route.query.carIds) {
            this.getOldData(this.$route.query.carIds)
        } else {
            this.form = {
                carList:[],
                transferAnnex:'',
                feeAnnex:'',
            }
        }
        this.state = this.$route?.query?.type??'write'

        const title =  this.$route?.query?.pageType??'内部过户申请'
        const route = Object.assign({}, this.$route, { title: `${title}` })
        this.$store.dispatch('tagsView/updateVisitedView', route)

    },
    methods: {
        cancel() {
            this.$router.back();
            this.$store.dispatch('tagsView/delView', this.$route);
        },
        async submitForm(type) {
            let that = this

            this.form.saveFlag = type

            if (0 == type) {
                that.commit()
            } else if (1 == type) {
                let result = await that.$refs.form.valid();

                if (result) {
                    that.commit()
                }
            }
        },

        commit() {
            let that = this
            this.buttonLoading = true;


            if (that.form.id) {
                carTransferEdit(that.form).then(res => {
                    this.buttonLoading = false;
                    if (0 == that.form.saveFlag) {
                        this.$modal.msgSuccess("保存成功");
                    } else if (1 == that.form.saveFlag) {
                        this.$modal.msgSuccess("提交成功");
                    }
                    this.$router.back();
                    this.$store.dispatch('tagsView/delView', this.$route);
                }).finally(e => {
                    this.buttonLoading = false;
                })
            } else {
                carTransferAdd(that.form).then(res => {
                    this.buttonLoading = false;
                    if (0 == that.form.saveFlag) {
                        this.$modal.msgSuccess("保存成功");
                    } else if (1 == that.form.saveFlag) {
                        this.$modal.msgSuccess("提交成功");
                    }
                    this.$router.back();
                    this.$store.dispatch('tagsView/delView', this.$route);
                }).finally(e => {
                    this.buttonLoading = false;
                })
            }
        },

        getOldData(carIds) {
            findByIds(carIds?carIds?.split(","):[]).then(res => {
                for (let item of res) {
                    item.sourceCarNo = item.carNo
                }
                this.$set(this.form, 'transferBeforeName', res[0].propertyRightName)
                this.$set(this.form, 'transferBeforeId', res[0].propertyRightId)
                this.$set(this.form, 'carList', res)

            })
        },
    }
};
</script>


<style lang="scss" scoped>
::v-deep .el-form--label-top .el-form-item__label {
    padding: 0;
}

.el-form-item {
    margin-bottom: 15px;
    width: 70%;
}

.icon_add {
    margin-left: 10px;
    cursor: pointer;
}

</style>
